import random

# 所有可能 出现的概率暂且相等
list = [{'name': 'A', 'value': 5.4, 'probability': 1/4}, {'name': 'B', 'value': 6.2, 'probability': 1/4},{'name': 'C', 'value': 7.0, 'probability': 1/4}, {'name': 'D', 'value': 7.8, 'probability': 1/4}]

# 计算排列
def caleCombination():
    combinationResult = []
    for i1 in range(4):
        for i2 in range(4):
            for i3 in range(4):
                for i4 in range(4):
                    for i5 in range(4):
                        for i6 in range(4):
                            # 结果的排列方式
                            combination = list[i1]['name']+list[i2]['name'] + \
                                list[i3]['name']+list[i4]['name'] + \
                                list[i5]['name']+list[i6]['name']
                            # 结果的排列方式 已排序 用于计算某种排列的次数
                            sortCombination = [list[i1]['name'], list[i2]['name'], list[i3]['name'], list[i4]['name'], list[i5]['name'], list[i6]['name']]
                            sortCombination.sort()
                            sortCombination = ''.join(sortCombination)
                            # 当前概率 值
                            probabilityValue = list[i1]['value']+list[i2]['value'] + list[i3]['value']+list[i4]['value'] + list[i5]['value']+list[i6]['value']
                            # 概率
                            probability = list[i1]['probability']*list[i2]['probability']*list[i3]['probability'] * list[i4]['probability'] * list[i5]['probability']*list[i6]['probability']
                            # 将所有结果装入排列组合的结果 combination: 排列组合 value: 爆伤值 times: 该组合出现的次数 probability: 出现该组合的概率 probabilityMuiTimes: 组合出现的次数乘概率
                            combinationItem = {'combination': sortCombination,'value': probabilityValue,'times': 1,'probability': probability,'probabilityMuiTimes': probability}
                            hasItem = False
                            for item in combinationResult:
                                if item['combination'] == sortCombination:
                                    combinationItem = item
                                    hasItem = True
                            if hasItem == False:
                                combinationResult.append(combinationItem)
                            else:
                                combinationItem['times'] += 1
                                combinationItem['probabilityMuiTimes'] = combinationItem['times'] * combinationItem['probability']
    return combinationResult

# 结果写入文件
def writeResultToFile(combinationResult):
    # 文件名
    fileName = '计算结果.csv'
    with open(fileName, "w") as md5File:
        md5File.write('组合' + ","+'值'+','+'次数'+','+'概率'+','+'概率x次数' + "\n")
        for item in combinationResult:
            md5File.write(item['combination'] + ","+str(round(item['value'], 2))+','+str(item['times'])+','+str(round(item['probability'], 9))+','+str(round(item['probabilityMuiTimes'], 9)) + "\n")

# 排序函数 将结果按组合乘概率降序
def sortValue(e):
    return e['probabilityMuiTimes']

combinationResult = caleCombination()
# 对结果排序
combinationResult.sort(reverse=True, key=sortValue)
print(combinationResult)
writeResultToFile(combinationResult)
